package com.bukkit.feverdream.noon;

import java.io.File;
import java.util.Timer;
import java.util.logging.Logger;

import org.bukkit.Server;
import org.bukkit.plugin.*;
import org.bukkit.plugin.java.*;

//amk
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import com.nijikokun.bukkit.Permissions.Permissions;

public class Noon extends JavaPlugin {

    public static Permissions Permissions = null;
    private String _mode = "";
    private Settings _settings = null;
    private Timer _tick = null;
    private SunshineTimerTask _timerTask = null;
    private int _rate = 20000; //milliseconds
    private final NPlayerListener playerListener = new NPlayerListener(this);
    private static final Logger _log = Logger.getLogger("Minecraft");

    public Noon(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File folder, File plugin, ClassLoader cLoader) {
        super(pluginLoader, instance, desc, folder, plugin, cLoader);
    }

    public void onEnable() {
        _settings = new Settings(this);
        setupPermissions();

        //initialize plugin
        if (_settings.loadSettings() && setMode(_settings.getMode())) {

            _log.info("[Noon] (based on code by Feverdream) loaded.");
        } else {
            _log.info("Configure your noon.properties file and restart.");
            _log.info("The key you need to configure is called 'always-time'.");
            _log.info("The key values Noon supports are day, sunset, night, sunrise, and off.");
            _log.info("[Noon] (based on code by Feverdream) loaded.");
        }

        //receive commands
        getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this);
    }

    public void setupPermissions() {
	Plugin test = getServer().getPluginManager().getPlugin("Permissions");
	if(Permissions == null) {
	    if(test != null) {
		Permissions = (Permissions)test;
	    } else {
		_log.info("[Noon] Permission system not enabled. Disabling plugin.");
		getServer().getPluginManager().disablePlugin(this);
	    }
	}
    }

    public void onDisable() {
        if (_tick != null) {
            _tick.cancel();
            _tick = null;
        }
         _log.info("[Noon] disabled.");
    }

    public Settings getSettings() {
        return _settings;
    }

    public boolean setMode(String mode) {
        boolean result = false;
        //input filtering
        if (mode.equalsIgnoreCase("day")
                || mode.equalsIgnoreCase("sunset")
                || mode.equalsIgnoreCase("night")
                || mode.equalsIgnoreCase("sunrise")
                || mode.equalsIgnoreCase("off")) {
            //set the private var
            _mode = mode;

            //update the settings
            _settings.setMode(_mode);
            _settings.saveSettings();

            //operate the timer
            if (!_mode.equalsIgnoreCase("off")) {
                runTimer();
            } else {
                stopTimer();
            }
            result = true;
        }
        return result;
    }

    public String getMode() {
        return _mode;
    }

    public Logger getLog() {
        return _log;
    }

    public void runTimer() {
        if (_timerTask == null) {
            _timerTask = new SunshineTimerTask(this);
        }

        if (_tick == null) {
            _tick = new Timer();
            _tick.schedule(_timerTask, 0, _rate);
        }
        //timer is already running
        _timerTask.setMode(_mode);
    }

    public void stopTimer() {
        if (_tick != null) {
            _tick.cancel();
        }
        _tick = null;
        _timerTask = null;
    }
}
